var app = require('../../utils/util.js');
import {
  userCenter,
  phone,
  bindTelAndPossWord,
  addressList,
  cooperation,
  bindController
} from '../../utils/request/apiurl'

import api from '../../utils/request/api'


Page({
	/**
	 * 页面的初始数据
	 */
  data: {    
    phone: '', //手机号
    code: '', //验证码
    oldPassword:'',
    newPassword: '',
    confirmPassword: '',
    iscode: '', //用于存放验证码接口里获取到的code
    codename: '获取验证码',
    uid: '',
    numCode: 60,//倒计时60s
    userData:'',
    adressList: {
      relationName: '', //收货人
      relationTel: '', //收货人电话
      cooperationNetwork: '', //合作网点
      cooperationTel: '', //联系电话
      networkAdress: '', //网点地址
      adressDetail: '', //详细地址
    },
    cooperationId: '', //网点id
    gId: '',//购物车id
    sessionKey:'',
  },
  
  //获取input输入框的值
  // 请输入手机号
  getPhoneValue: function (e) {
    this.setData({
      phone: e.detail.value,
      'adressList.relationTel': e.detail.value,
    })
  },
  //获取sessionKey
  sessionKeyData(){
    var that=this
    wx.login({
      success: function (res) {
        console.log(res,'resresres')
        var data={
          code: res.code
        }
        api.post(bindController.getSessionKey, data).then(row => {
          console.log(row)
            that.setData({
              sessionKey: row
            })

        })
      }
    })
  },
  //获取微信用户自带的手机号
  getPhoneNumber(e) {
    console.log(e)
    var that = this;
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      var source={
        encrypdata:e.detail.encryptedData,
        ivdata: e.detail.iv,
        sessionkey: that.data.sessionKey
      }
      api.post(bindController.getTel, source).then(res => {
        console.log(res);
        if(res){
          that.setData({
            phone: res.data,
            'adressList.relationTel': res.data,
          })
        }else{
          wx.showToast({
            title: '获取失败',
            icon: 'none'
          })
        }
       
        
      })
    }
  },
  // 请输入验证码
  getCodeValue: function (e) {
    // console.log(e)
    this.setData({
      code: e.detail.value
    })
  },
  getCode: function () {
    var a = this.data.phone;
    var _this = this;
    if (_this.data.numCode < 60 && _this.data.numCode > 0) {
      return wx.showToast({
        title: '请稍后发送验证码',
        icon: 'none'
      })
    }
    // var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    var myreg = /^1(3|4|5|6|7|8|9)\d{9}$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      var source = {
        uid: this.data.uid,
        phone: this.data.phone,
      }
      // console.log(source)
      api.post(phone.createCode, source).then(res => {
        // console.log(res)
        // this.setData({
        // 	iscode:res,
        // })
        wx.showToast({
          title: '发送成功',
          icon: 'none',
          duration: 2000
        })
        // var num = 60;
        var timer = setInterval(function () {
          // num--;
          _this.setData({
            numCode: _this.data.numCode - 1,
          })
          if (_this.data.numCode <= 0) {
            clearInterval(timer);
            _this.setData({
              codename: '重新发送',
              disabled: false,
              numCode: 60,
            })

          } else {
            _this.setData({
              codename: _this.data.numCode + "s"
            })
          }
        }, 1000)

      }).catch(err => {
        // console.log(err)
        wx.showToast({
          title: err.message,
          icon: 'none'
        })
      })

    }
  },
  bindRelationName(e) {
    this.setData({
      'adressList.relationName': e.detail.value
    })
  },
  changeOldPassword:function(e){
    this.setData({
      oldPassword: e.detail.value
    })
  },
  changeNewPassword: function (e) {
    this.setData({
      newPassword: e.detail.value
    })
  },
  changecConfirmPassword: function (e) {
    this.setData({
      confirmPassword: e.detail.value
    })
  },
  cooperationNetwork() {
    wx.navigateTo({
      url: '../cooperation-network/cooperation-network?gId=' + this.data.gId + '&relationName=' + this.data.adressList.relationName + "&linkPhone=" + this.data.adressList.relationTel
    })
  },
  //获取验证码
  getVerificationCode() {
    this.getCode();
    var _this = this
    _this.setData({
      disabled: true
    })
  },
  //提交表单信息
  save: function () {
    console.log(this.data.newPassword)
    if (this.data.code==''){
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none'
      })
      return false
    }
    if (this.data.newPassword=='') {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none'
      })
      return false
    } else if (this.data.newPassword.length!=6) {
      wx.showToast({
        title: '密码位数不对',
        icon: 'none'
      })
      return false
    }
    if (this.data.newPassword != this.data.confirmPassword){
      wx.showToast({
        title: '确认密码和新密码不一致',
        icon: 'none'
      })
      return false
    }
    let source = {
      uid: this.data.uid,
      code: this.data.code,
    }
 
    api.post(phone.verifyCode, source).then(res => {
      // console.log(res)
      // debugger
      if (res == '验证码正确') {
       
        let source = {
          uid: this.data.uid,
          telephone: this.data.phone,
          password: this.data.newPassword,
          oldPassword : this.data.oldPassword
        }
        // console.log(source)
        api.post(bindTelAndPossWord.list, source).then(res => {
          console.log(res,'bindTelAndPossWord.list')
          // this.getUserInfor(1)
          this.addNewAddress()
          console.log(2222)
          // wx.navigateTo({
          //   url: '../personalInfo/personalInfo',
          // }) //页面跳转         
        }).catch(err => {
          // console.log(err)
          wx.showToast({
            title: err.message,
            icon: 'none'
          })
        })
        
      }
    }).catch(err => {
      // console.log(err)
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })

  },
  //获取网点信息
  cooperationList() {
    var source = {
      id: this.data.cooperationId
    }
    api.post(cooperation.getCooperationById, source).then(res => {
      // console.log(res)
      this.setData({
        'adressList.cooperationNetwork': res.cooperationName,
        'adressList.cooperationTel': res.linkPhone,
        'adressList.networkAdress': res.cooperationAddress,
      })
    }).catch(err => {
      // console.log(err)
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  },
  //添加地址
  addNewAddress() {
    if (this.data.adressList.relationName == '') {
      wx.showToast({
        title: '收货人不能为空',
        icon: 'none'
      })
      return false
    }
    if (this.data.adressList.relationTel == '') {
      wx.showToast({
        title: '收货人电话不能为空',
        icon: 'none'
      })
      return false
    }
    if (this.data.adressList.cooperationNetwork == '') {
      wx.showToast({
        title: '合作网点不能为空',
        icon: 'none'
      })
      return false
    }
    var source = {
      uid: this.data.uid,
      name: this.data.adressList.relationName,
      phone: this.data.adressList.relationTel,
      cooperationId: this.data.cooperationId,
    }
    // console.log(source)
    api.post(addressList.add, source).then(res => {
      console.log(res)
      // if (res > 0) {   
      //   this.getUserInfor(1)
      // }
      this.getUserInfor(1)
    }).catch(err => {
      // console.log(err)
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  },
	/**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
    //获取个人信息
    this.getUserInfor(2)
    if (this.data.cooperationId != '' && this.data.cooperationId != undefined && this.data.cooperationId != null) {
      this.cooperationList();
    }
  },
  getUserInfor:function(i){
    api.post(userCenter.userData, {
      uid: this.data.uid || wx.getStorageSync('userInfo').uid
    }).then(res => {
      var userInfo = wx.getStorageSync('userInfo')
      userInfo['isColonel'] = res.isColonel;
      userInfo['isRealname'] = res.isRealname;
      userInfo['isPayPassword'] = res.isPayPassword;
      // userInfo['isColonel'] = res.isColonel //0不是团长，1是团长
      userInfo['role'] = res.role; //0不是快递员，1是快递员
      userInfo['rank'] = res.rank; //0不是快递员，1是快递员
      userInfo['sellCount'] = res.sellCount;
      wx.setStorageSync('userInfo', userInfo);
      this.setData({
        userData: res,
        phone: res.phone,
        'adressList.relationTel': res.phone,
        uid: res.id
      })
      
      if(i==1){
        console.log(1111111)
        // wx.navigateBack({
        //   delta: 1
        // })
        wx.navigateBack({ changed: true })
      }
    }).catch(err => {
      wx.showToast({
        title: err.message,
        icon: 'none'
      })
    })
  },
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function () {

  },

	/**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function () {
    if (this.data.cooperationId != '' && this.data.cooperationId != undefined && this.data.cooperationId != null) {
      this.cooperationList();
    }
    this.sessionKeyData()
  },

	/**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function () {

  },

	/**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function () {

  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {

  },

	/**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {

  },

	/**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function () {

  },

})
